<?php 
// +----------------------------------------------------------------------
// | RXThinkCMF_EVTP5.1_PRO前后端分离旗舰版框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2018~2021 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | 作者: ZhangXuanNeng <rxthinkcmf@163.com>
// +----------------------------------------------------------------------
// | 免责声明:
// | 本软件框架禁止任何单位和个人用于任何违法、侵害他人合法利益等恶意的行为，禁止用于任何违
// | 反我国法律法规的一切平台研发，任何单位和个人使用本软件框架用于产品研发而产生的任何意外
// | 、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、附带
// | 或衍生的损失等)，本团队不承担任何法律责任。本软件框架只能用于公司和个人内部的法律所允
// | 许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

namespace app\admin\service;


use app\admin\model\PaymentAccount;
use app\common\controller\SubWxPay;

/**
 * 收款账户管理-服务类
 * @author ZhangXuanNeng
 * @since: 2023/08/25
 * Class PaymentAccountService
 * @package app\admin\service
 */
class PaymentAccountService extends BaseService
{
    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new PaymentAccount();
    }
	
	/**
     * 获取数据列表
     * @return array
     * @since 2023/08/25
     * @author ZhangXuanNeng
     */
    public function getList()
    {
        $param = request()->param();

        // 查询条件
        $map = [];
		if(isset($param['company']) && !empty($param['company'])){
		    $map[] = ['company','like','%'.$param['company'].'%'];
        }
        if(isset($param['pay_way_id']) && !empty($param['pay_way_id'])){
            $map[] = ['pay_way_id','=',$param['pay_way_id']];
        }
        return parent::getList($map); // TODO: Change the autogenerated stub
    }

    /**
     * 添加或编辑
     * @return mixed
     * @since 2020/11/19
     * @author ZhangXuanNeng
     */
    public function edit()
    {
        // 获取参数
        $argList = func_get_args();
        // 查询条件
        $data = isset($argList[0]) ? $argList[0] : [];
        // 是否打印SQL
        $is_sql = isset($argList[1]) ? $argList[1] : false;
        if (!$data) {
            $data = request()->param();
        }
        $error = '';
        $params = [];
        $arr = array(
            'pay_way_id'=> $data['pay_way_id'],
            'company'   => $data['company'],
        );
        $payWayTemplate = new PayWayTemplateService();
        $template = $payWayTemplate->getTemplateList($data['pay_way_id']);
        foreach ($template as $k=>$v){
            $params[$v['key']] = $data[$v['key']];
            if($v['is_account'] == 1){
                $arr['mchid'] = $data[$v['key']];
            }
        }
        if($data['pay_way_id'] == 5 || $data['pay_way_id'] == 6){
            $subWx = new SubWxPay();
            $sub_account = $subWx->subAccount($params);
            if($sub_account['return_code'] != 'SUCCESS' || $sub_account['result_code'] != 'SUCCESS'){
                return message('添加分账接收方失败:'.$sub_account['return_msg'], false);
            }
        }
        $arr['params'] = json_encode($params);
        if(isset($data['id'])){
            $arr['id'] = $data['id'];
        }
        $rowId = $this->model->edit($arr, $error, $is_sql);
        if ($rowId) {
            return message();
        }
        return message($error, false);
    }

    public function getAll(){
        $param = request()->param();

        // 查询条件
        $map = [];
        if(isset($param['pay_way_id']) && !empty($param['pay_way_id'])){
            $map[] = ['pay_way_id','=',$param['pay_way_id']];
        }
        $result = $this->model->where($map)->order('id desc')->column("id");
        $list = [];
        if (is_array($result)) {
            foreach ($result as $val) {
                $info = $this->model->getInfo($val);
                $list[] = $info;
            }
        }

        //获取数据总数
        $count = $this->model->count();

        //返回结果
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $list,
            "count" => $count,
        );
        return $message;
    }


}